<?php
/**
 * 直播接口
 * @author XQ
 */
namespace app\api\controller\v1;

use app\common\model\ChatClient;
use app\facade\Live AS LiveFacade;
use think\Db;
use GatewayWorker\Lib\Gateway;
class Live extends Base
{
    //列表
    public function getlist(){
        try {
            $result = LiveFacade::getList();
            return j(0, '成功',$result);
        } catch (\Exception $e) {
            return j(1, $e->getMessage());
        }
    }
	//详情
	public function getInfo(){
    	$id = input('id',0);//直播间列表
		try {
			$result = LiveFacade::getInfo($id);
			if ($result) {
				return j(0, '成功',$result);
			}

			return j(1, '失败');
		} catch (\Exception $e) {
			return j(1, $e->getMessage());
		}
	}
	//聊天
	public function addchat(){
		$userId = $this->checkUserToken();
		$live_id = input('live_id',0);
		$content = input('content',0);
		try {
			$result = LiveFacade::addchat($userId,$live_id,$content);
			$room = ChatClient::field('client_id')->where('room_id',$live_id)->select()->toArray();
			$ids = array_column($room,'client_id');
			$data = array(
				'type'=>1
			);
			@Gateway::sendToAll(json_encode($data),$ids);
			if ($result) {
				return j(0, '成功',$result);
			}

			return j(1, '失败');
		} catch (\Exception $e) {
			return j(1, $e->getMessage());
		}
	}
	//聊天列表
	public function chatList(){
		$live_id = input('live_id',0);
		try {
			$result = LiveFacade::chatList($live_id);
			return j(0, '成功',$result);
		} catch (\Exception $e) {
			return j(1, $e->getMessage());
		}
	}
	//直播点赞
	public function addLike(){
		$userId = $this->checkUserToken();
		$live_id = input('live_id',0);
		try {
			$result = LiveFacade::addLike($userId,$live_id);
			$room = ChatClient::field('client_id')->where('room_id',$live_id)->select()->toArray();
			$ids = array_column($room,'client_id');
			$data = array(
				'type'=>2
			);
			@Gateway::sendToAll(json_encode($data),$ids);
			return j(0, '成功',$result);
		} catch (\Exception $e) {
			return j(1, $e->getMessage());
		}
	}

	//礼物列表
	public function giftList(){
		try {
			$result = LiveFacade::giftList();
			return j(0, '成功',$result);
		} catch (\Exception $e) {
			return j(1, $e->getMessage());
		}
	}
}
